package com.example.neusoft.fragmenttest.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.neusoft.fragmenttest.R;
import com.example.neusoft.fragmenttest.adatper.ArticleAdapter;
import com.example.neusoft.fragmenttest.bean.ArticleBean;
import com.example.neusoft.fragmenttest.iface.ArticleListener;
import com.example.neusoft.fragmenttest.model.ArticleModel;

import java.util.List;


/**
 * Created by Neusoft on 2017/4/9.
 */

public class Fragment1 extends Fragment  {
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private ArticleAdapter mAdapter;

    List<ArticleBean> list=null;
    Context context;

    ArticleListener articleListener=new ArticleListener() {

        @Override
        public void onResponse(List<ArticleBean> beanList) {
            list=beanList;
            mAdapter.setList(list);
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(context, "fail:"+msg, Toast.LENGTH_SHORT).show();
        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       View view=inflater.inflate(R.layout.fragment1,container,false);
       // tvinfo=(TextView)view.findViewById(R.id.textView8);
        //tvinfo.setText("微信");
        //Toast.makeText(getActivity(), "微信", Toast.LENGTH_SHORT).show();
        context=getActivity();
        initRecyclerView(view);

        ArticleModel articlemodel=new ArticleModel();
        articlemodel.getResultList("article",2,"rv1l2tidbomann2qn86nln6ur2",articleListener);
        return view;
    }

    private void initRecyclerView(View  view) {
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        //创建默认的线性LayoutManager
        layoutManager = new LinearLayoutManager(getActivity());
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        recyclerView.setHasFixedSize(true);
        //创建并设置Adapter
        //list = getData();
        mAdapter = new ArticleAdapter(getActivity());
        mAdapter.setList(list);
        recyclerView.setAdapter(mAdapter);
    }


}
